package com.wanbangee.marksystem.service.imp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.wanbangee.marksystem.dao.UGradeSubjectRelationDao;
import com.wanbangee.marksystem.entities.UGrade;
import com.wanbangee.marksystem.entities.UGradeSubjectRelation;
import com.wanbangee.marksystem.entities.USubject;
import com.wanbangee.marksystem.service.UGradeSubjectRelationService;
@Service
public class UGradeSubjectRelationServiceImp implements UGradeSubjectRelationService{
	
	@Autowired
	private UGradeSubjectRelationDao uGradeSubjectRelationDao;

	@Override
	public List<UGradeSubjectRelation> selectSubjectBygraId(int schId, int graId) {
		List<UGradeSubjectRelation> lists = this.uGradeSubjectRelationDao.selectSubjectBygraId(schId, graId);
		return lists;
	}

	@Transactional
	@Override
	public Map<String, Object> insertSubjectIntoGrade(@RequestBody JSONArray jsonArray){
		//先删除
		JSONObject jb1 = jsonArray.getJSONObject(0);
		this.uGradeSubjectRelationDao.deleteSubjectFromGrade(jb1.getInteger("graId"),jb1.getInteger("schId"));
		
		
		//后增
		Map<String, Object> map = new HashMap<String, Object>();
		for(int i = 0; i<jsonArray.size(); i++) {
			JSONObject jb = jsonArray.getJSONObject(i);
			UGradeSubjectRelation ugs = new UGradeSubjectRelation();
			ugs.setGraId(jb.getInteger("graId"));
			ugs.setGraNm(jb.getString("graNm"));
			ugs.setSubId(jb.getInteger("subId"));
			ugs.setSubNm(jb.getString("subNm"));
			ugs.setSchId(jb.getInteger("schId"));
			ugs.setSchNm(jb.getString("schNm"));
			ugs.setAddTime(jb.getString("addTime"));
			ugs.setAddUserId(jb.getInteger("addUserId"));
			this.uGradeSubjectRelationDao.insertSubjectIntoGrade(ugs);
			map.put("code", 1001);
			map.put("message", "接口成功");
		}
	
		return map;
	}

	@Override
	public List<UGrade> selectGradeBySchId(int schId) {
		List<UGrade> list = this.uGradeSubjectRelationDao.selectGradeBySchId(schId);
		return list;
	}

}
